"""
样本管理路由
"""
from typing import Optional, List
from fastapi import APIRouter, Depends, HTTPException, Form, UploadFile, File
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
from database import get_db
from schemas import (
    SampleTransferRequest, SampleCollectRequest
)
from services import DataTransferService
from response_utils import success_response, error_response

router = APIRouter(prefix="/api/v1", tags=["samples"])

# 数据传输服务实例
data_transfer_service = DataTransferService()

@router.post("/samples/transfer")
async def transfer_samples(
    source_node: str = Form(...),
    target_node: str = Form(...),
    sample_ids: List[str] = Form(...),
    transfer_mode: str = Form("incremental"),
    db: Session = Depends(get_db)
):
    """传输样本数据"""
    try:
        request_data = SampleTransferRequest(
            source_node=source_node,
            target_node=target_node,
            sample_ids=sample_ids,
            transfer_mode=transfer_mode
        )
        result = data_transfer_service.transfer_samples(db, request_data)
        return JSONResponse(
            content=success_response(data=result, message="样本传输成功"),
            status_code=201
        )
    except ValueError as e:
        return JSONResponse(
            content=error_response(str(e)),
            status_code=400
        )

@router.post("/samples/collect")
async def collect_samples(
    node_id: str = Form(...),
    collection_type: str = Form("auto"),
    sample_count: int = Form(100),
    filters: Optional[dict] = Form(None),
    db: Session = Depends(get_db)
):
    """归集样本数据"""
    try:
        request_data = SampleCollectRequest(
            node_id=node_id,
            collection_type=collection_type,
            sample_count=sample_count,
            filters=filters or {}
        )
        result = data_transfer_service.collect_samples(db, request_data)
        return JSONResponse(
            content=success_response(data=result, message="样本归集成功"),
            status_code=201
        )
    except ValueError as e:
        return JSONResponse(
            content=error_response(str(e)),
            status_code=400
        ) 